﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.DataModel;
using System.Data.Entity;

namespace Models.DataStore
{
    public partial class Ctrl : Core
    {
        public async Task<int> AddPort(Port port)
        {
            if (!string.IsNullOrEmpty(port.PortNumber))
            {
                port.CreatedOn = DateTime.Now;
                port.ModifiedOn = DateTime.Now;
                port.PortId = Guid.NewGuid();
                db.Ports.Add(port);
                return await db.SaveChangesAsync();
            }
            return 0;
        }
        public async Task<int> EditPort(Port port)
        {
            try
            {
                db.Entry(port).State = EntityState.Modified;
                return await db.SaveChangesAsync();
            }
            catch
            {
                return 0;
            }
        }
        public async Task<int> DeletePort(Guid? portid)
        {
            Port port = db.Ports.FirstOrDefault(p => p.PortId == portid);
            db.Ports.Remove(port);
            return await db.SaveChangesAsync();
        }
        public IQueryable<Port> GetlstPort()
        {
            var lstPort = db.Ports;
            return lstPort;
        }
        public Port GetObjPort(Guid portid)
        {
            var lstPort = db.Ports.FirstOrDefault(p => p.PortId == portid);
            return lstPort;
        }

        public bool CheckExistPortNumber(string portNumber)
        {
            return db.Ports.Count(p => p.PortNumber == portNumber) > 0;
        }

        public IQueryable<Port> GetlstPortbyName(string portName)
        {
            try
            {
                var lstPort = db.Ports.Where(p => p.Name.Contains(portName));
                return lstPort;
            }
            catch (Exception ex) { return null; }
        }
    }
}
